var officeMap = new Array();

function setComerc() {
    ableSetComerc = true;
}

function addOffice(latlng, map) {
    ableSetComerc = false;
    var request = makeRequest();
    request.open("POST", "/storeHouses", false);
    var office = new Office();
    office.lat = latlng.lat();
    office.lng = latlng.lng();
    request.send(office2Text(office));
    screenMoveEnd(map);
}

function moveOffice(office) {
    var request = makeRequest();
    request.open("POST", "/storeHouses", true);
    request.send(office2Text(office));
}

function createOffice(map, latlng, office) {
    var houseIcon = new GIcon(G_DEFAULT_ICON);
    if (office.type == 'RENT') {
        houseIcon.image = "/img/comerc24.png";
    } else if (office.type == 'SALE') {
        houseIcon.image = "/img/comerc24.png";
    }

    houseIcon.iconSize = new GSize(24, 24);

    var markerOptions;
    //если свой объект
    if (userid == office.userid) {
        markerOptions = { icon:houseIcon, draggable:true};
    } else {
        markerOptions = { icon:houseIcon, draggable:false};
    }

    var marker = new GMarker(latlng, markerOptions);
    //финт ушами, иначе не получим flat
    marker.office = office;
    office.marker = marker;

    GEvent.addListener(marker, "click", function() {
        //        var zoom = map.getZoom();
        //        map.setCenter(marker.getLatLng(), zoom);
        centerOffice(marker.office);
    });

    //если свой объект
    if (userid == office.userid) {
        GEvent.addListener(marker, "dragend", function() {
            office.lat = marker.getLatLng().lat();
            office.lng = marker.getLatLng().lng();
            moveOffice(office);
        });
    } else {
        GEvent.addListener(marker, "mouseover", function() {
            map.openInfoWindowHtml(marker.getLatLng(), createOfficeInfo(office));
        });
    }

    return marker;
}

function updateMarkerOffice(map, latlng, office) {
    var houseIcon = new GIcon(G_DEFAULT_ICON);
    if (office.type == 'RENT') {
        office.marker.setImage("/img/comerc24.png");
    } else if (office.type == 'SALE') {
        office.marker.setImage("/img/comerc24.png");
    }
    office.marker.setLatLng(latlng);
}

function centerOffice(office) {
    var zoom = globalmap.getZoom();
    globalmap.setCenter(new GLatLng(office.lat, office.lng, zoom), zoom);
    setResponseToElement('/getoffice.do?officeid=' + office.id, 'properties');
}

function createOfficeInfo(office) {
    var text = '<table onclick="setResponseToElement(\'/getoffice.do?officeid=' + office.id + '\',\'properties\')"><tr>';
    text += '<td>' + '<img src="/img/comerc24.png"/>' + '</td>';
    text += '<td>' + office.roomNumber + 'к ' + office.price + office.currency + '</td>';
    text += '</tr></table>';
    return text;
}

function deleteOffice(officeid) {
    if(confirm('Удалить объект')) {
        globalmap.removeOverlay(officeMap[officeid].marker);
        officeMap[officeid] = undefined;
        setResponseToElement('/deleteoffice.do?officeid=' + officeid, 'properties');
    }
}